import java.sql.Time;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Tour
{
    private Map<Integer, List<Event>> events; // events on our tour
    private Place startPlace, // place the tour starts at
            endPlace;// place the tour ends at
    private int activityLevel, // estimated activity level of tour
            maxSize; // maximum size of the tour
            
    /**
     * Initialize the tour
     * 
     * @param startPlace
     *            place the tour starts at
     * @param endPlace
     *            place the tour ends at
     */
    public Tour(Place startPlace, Place endPlace, int actLevel, int maxSize)
    {
        setStartPlace(startPlace);
        setEndPlace(endPlace);
        setActivityLevel(actLevel);
        setMaxSize(maxSize);
        
        events = new HashMap<Integer, List<Event>>();
    }
    
    /**
     * @return place the tour starts at
     */
    public Place getStartPlace()
    {
        return startPlace;
    }
    
    /**
     * @return place the tour ends at
     */
    public Place getEndPlace()
    {
        return endPlace;
    }
    
    /**
     * @return estimated activity level of tour
     */
    public int getActivityLevel()
    {
        return activityLevel;
    }
    
    /**
     * @return max size of tour
     */
    public int getMaxSize()
    {
        return maxSize;
    }
    
    /**
     * @return all events on the tour
     */
    public List<Event> getEvents()
    {
        List<Event> allEvents = new LinkedList<Event>();
        
        for (List<Event> eventList : events.values())
        {
            allEvents.addAll(eventList);
        }
        
        return allEvents;
    }
    
    /**
     * Set starting place of tour
     * 
     * @param start
     *            place tour starts at
     */
    private void setStartPlace(Place start)
    {
        startPlace = start;
    }
    
    /**
     * Set ending place of tour
     * 
     * @param end
     *            place tour ends at
     */
    private void setEndPlace(Place end)
    {
        endPlace = end;
    }
    
    /**
     * Set estimated activity level of tour
     * 
     * @param actLevel
     *            estimated activity level of tour
     */
    private void setActivityLevel(int actLevel)
    {
        this.activityLevel = actLevel;
    }
    
    /**
     * Set max size of tour
     * 
     * @param max
     *            max size of tour
     */
    private void setMaxSize(int max)
    {
        this.maxSize = max;
    }
    
    /**
     * Add an event to the tour
     * 
     * @param day
     *            day the event is on
     * @param event
     *            event we are adding
     */
    public void addEvent(int day, Event event)
    {
        List<Event> daysEvents = events.get(day);
        
        // if the days hasn't had events added to it yet
        if (daysEvents == null)
        {
            daysEvents = new LinkedList<Event>();
            events.put(day, daysEvents);
        }
        
        // if event isn't already added
        if (!daysEvents.contains(event))
            daysEvents.add(event);
    }
    
    /**
     * to string method for printing
     */
    public String toString()
    {
        String s = "";
        s += "Tour:\n\tActivity Level: " + getActivityLevel() + "\n\tMax Size " + getMaxSize() + "\n\tStart Location: " + getStartPlace().toString()
                + "\n\tEnd " + "Location: " + getEndPlace().toString() + "\n\tEvents: \n";
        
        // go through days
        for (int i : events.keySet())
        {
            s += "day " + i + ":\n";
            
            // go through events in day i
            for (Event e : events.get(i))
            {
                s += e.toString();
            }
        }
        
        return s;
    }
    
    /**
     * main for unit testing
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        Place p1 = new Place("1");
        Place p2 = new Place("2");
        
        Time t1 = new Time(0000);
        Time t2 = new Time(10000);
        Time t3 = new Time(100000);
        
        Event e1 = new Event(EventType.ACTIVITY, t1, t2);
        Event e2 = new Event(EventType.DINING, t2, t3);
        
        Tour tour1 = new Tour(p1, p2, 10, 100);
        Tour tour2 = new Tour(p2, p1, 2, 12);
        tour1.addEvent(0, e1);
        tour2.addEvent(0, e2);
        tour1.addEvent(0, e2);
        
        System.out.println(tour1);
        System.out.println(tour2);
    }
}
